// 主要JavaScript功能
document.addEventListener('DOMContentLoaded', function() {
    // 初始化应用
    initApp();
    
    // 初始化移动端菜单
    initMobileMenu();
});

function initApp() {
    // 设置导航栏活动状态
    setActiveNavItem();
    
    // 初始化警告消息自动隐藏
    initAlerts();
    
    // 初始化表单验证
    initFormValidation();
}

function initMobileMenu() {
    // 移动端菜单展开/收起功能
    const menuItems = document.querySelectorAll('.nav-menu > li');
    
    menuItems.forEach(item => {
        const link = item.querySelector('a');
        const submenu = item.querySelector('ul');
        
        if (submenu) {
            link.addEventListener('click', function(e) {
                // 只在移动端阻止默认行为
                if (window.innerWidth <= 768) {
                    e.preventDefault();
                    e.stopPropagation();
                    
                    // 关闭其他展开的菜单
                    menuItems.forEach(otherItem => {
                        if (otherItem !== item && otherItem.classList.contains('open')) {
                            otherItem.classList.remove('open');
                        }
                    });
                    
                    // 切换当前菜单
                    item.classList.toggle('open');
                }
            });
        }
    });
    
    // 点击遮罩层关闭菜单
    const navToggle = document.getElementById('nav-toggle');
    const navOverlay = document.querySelector('.nav-overlay');
    
    if (navOverlay && navToggle) {
        navOverlay.addEventListener('click', function() {
            navToggle.checked = false;
        });
    }
    
    // 监听窗口大小变化，重新初始化菜单
    window.addEventListener('resize', function() {
        if (window.innerWidth > 768) {
            // 桌面端时移除所有open类
            menuItems.forEach(item => {
                item.classList.remove('open');
            });
        }
    });
}

function setActiveNavItem() {
    // 根据当前URL设置导航栏活动状态
    const currentPath = window.location.pathname;
    const navLinks = document.querySelectorAll('.nav-menu a');
    
    navLinks.forEach(link => {
        if (link.getAttribute('href') === currentPath) {
            link.classList.add('active');
        }
    });
}

function initAlerts() {
    // 自动隐藏警告消息
    const alerts = document.querySelectorAll('.alert');
    alerts.forEach(alert => {
        setTimeout(() => {
            alert.style.opacity = '0';
            setTimeout(() => {
                alert.remove();
            }, 300);
        }, 5000);
    });
}

function initFormValidation() {
    // 基础表单验证
    const forms = document.querySelectorAll('form');
    forms.forEach(form => {
        form.addEventListener('submit', function(e) {
            const requiredFields = form.querySelectorAll('[required]');
            let isValid = true;
            
            requiredFields.forEach(field => {
                if (!field.value.trim()) {
                    isValid = false;
                    field.classList.add('error');
                } else {
                    field.classList.remove('error');
                }
            });
            
            if (!isValid) {
                e.preventDefault();
                showMessage('请填写所有必填字段', 'error');
            }
        });
    });
}

function showMessage(message, type = 'info') {
    // 显示消息
    const messageDiv = document.createElement('div');
    messageDiv.className = `alert alert-${type}`;
    messageDiv.textContent = message;
    
    const container = document.querySelector('.container');
    container.insertBefore(messageDiv, container.firstChild);
    
    // 自动隐藏
    setTimeout(() => {
        messageDiv.remove();
    }, 5000);
}

// 工具函数
function formatDate(date) {
    return new Date(date).toLocaleDateString('zh-CN');
}

function truncateText(text, length = 100) {
    if (text.length <= length) return text;
    return text.substring(0, length) + '...';
} 